{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 调整pdf页面大小不一的问题"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 检查pdf页面的大小"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Page 1: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 2: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 3: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 4: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 5: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 6: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 7: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 8: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 9: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 10: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 11: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 12: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 13: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 14: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 15: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 16: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 17: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 18: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 19: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 20: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 21: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 22: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 23: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 24: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 25: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 26: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 27: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 28: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 29: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 30: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 31: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 32: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 33: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 34: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 35: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 36: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 37: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 38: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 39: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 40: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 41: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 42: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 43: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 44: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 45: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 46: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 47: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 48: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 49: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 50: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 51: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 52: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 53: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 54: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 55: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 56: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 57: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 58: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 59: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 60: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 61: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 62: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 63: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 64: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 65: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 66: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 67: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 68: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 69: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 70: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 71: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 72: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 73: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 74: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 75: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 76: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 77: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 78: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 79: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 80: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 81: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 82: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 83: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 84: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 85: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 86: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 87: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 88: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 89: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 90: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 91: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 92: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 93: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 94: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 95: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 96: Width = 1064.0 pt, Height = 1536.0 pt\n",
      "Page 97: Width = 1064.0 pt, Height = 1536.0 pt\n"
     ]
    }
   ],
   "source": [
    "import fitz  # PyMuPDF\n",
    "\n",
    "def get_pdf_page_sizes(pdf_path):\n",
    "    # 打开PDF文件\n",
    "    document = fitz.open(pdf_path)\n",
    "    \n",
    "    # 遍历每一页，输出页面尺寸\n",
    "    for page_number in range(len(document)):\n",
    "        page = document[page_number]\n",
    "        rect = page.rect  # 页面矩形对象，包含页面的尺寸信息\n",
    "        width = rect.width\n",
    "        height = rect.height\n",
    "        print(f\"Page {page_number + 1}: Width = {width} pt, Height = {height} pt\")\n",
    "\n",
    "# 使用示例\n",
    "pdf_path = \"/Users/wanghao4799/Documents/附件3.pdf\"  # 替换为你的PDF文件路径\n",
    "get_pdf_page_sizes(pdf_path)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 调整页面的大小尺寸"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "PDF已保存到 /Users/wanghao4799/Documents/output.pdf\n"
     ]
    }
   ],
   "source": [
    "import fitz  # PyMuPDF\n",
    "\n",
    "def resize_pdf_pages(input_pdf_path, output_pdf_path, target_width, target_height):\n",
    "    # 打开输入的PDF文件\n",
    "    document = fitz.open(input_pdf_path)\n",
    "    \n",
    "    # 创建一个新的PDF文档\n",
    "    new_pdf = fitz.open()\n",
    "\n",
    "    # 遍历每一页并调整页面尺寸\n",
    "    for page_number in range(len(document)):\n",
    "        page = document[page_number]\n",
    "        rect = page.rect\n",
    "        \n",
    "        # 计算缩放比例\n",
    "        x_scale = target_width / rect.width\n",
    "        y_scale = target_height / rect.height\n",
    "        scale_matrix = fitz.Matrix(x_scale, y_scale)\n",
    "        \n",
    "        # 获取页面内容的图像\n",
    "        pixmap = page.get_pixmap(matrix=scale_matrix)\n",
    "        \n",
    "        # 创建一个新的页面\n",
    "        new_page = new_pdf.new_page(width=target_width, height=target_height)\n",
    "        \n",
    "        # 将图像放置到新的页面上\n",
    "        new_page.insert_image(new_page.rect, pixmap=pixmap)\n",
    "\n",
    "    # 保存新的PDF文件\n",
    "    new_pdf.save(output_pdf_path)\n",
    "    new_pdf.close()\n",
    "\n",
    "    print(f\"PDF已保存到 {output_pdf_path}\")\n",
    "\n",
    "# 使用示例\n",
    "input_pdf_path = \"/Users/wanghao4799/Documents/input.pdf\"  # 替换为你的输入PDF文件路径\n",
    "output_pdf_path = \"/Users/wanghao4799/Documents/output.pdf\"  # 替换为输出PDF文件路径\n",
    "target_width = 1064  # 目标宽度（以点为单位）\n",
    "target_height = 1536  # 目标高度（以点为单位）\n",
    "\n",
    "resize_pdf_pages(input_pdf_path, output_pdf_path, target_width, target_height)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 【转换】pdf贴入PPT"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "PDF文件中的所有页面已成功保存为图像，保存在: output_images\n"
     ]
    }
   ],
   "source": [
    "from pdf2image import convert_from_path\n",
    "import os\n",
    "\n",
    "def pdf_to_images(pdf_path, output_folder, image_format=\"png\"):\n",
    "    # 创建输出文件夹（如果不存在）\n",
    "    if not os.path.exists(output_folder):\n",
    "        os.makedirs(output_folder)\n",
    "\n",
    "    # 将PDF转换为图像\n",
    "    images = convert_from_path(pdf_path)\n",
    "    \n",
    "    # 保存图像\n",
    "    for i, image in enumerate(images):\n",
    "        image_filename = f\"{output_folder}/page_{i + 1}.{image_format}\"\n",
    "        image.save(image_filename)\n",
    "\n",
    "    print(f\"PDF文件中的所有页面已成功保存为图像，保存在: {output_folder}\")\n",
    "\n",
    "# 示例调用\n",
    "pdf_path = \"/Users/wanghao4799/Documents/目录.pdf\"  # 替换为你的PDF文件路径\n",
    "output_folder = \"output_images\"  # 输出图像的文件夹\n",
    "pdf_to_images(pdf_path, output_folder)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 【注释】对图片添加标题输出为PPT"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from PIL import Image\n",
    "from pptx import Presentation\n",
    "from pptx.util import Cm\n",
    "from pptx.dml.color import RGBColor\n",
    "\n",
    "def convert_images_to_ppt(input_folder, output_ppt):\n",
    "    # 获取文件夹中的所有图片\n",
    "    image_files = [f for f in os.listdir(input_folder) if os.path.isfile(os.path.join(input_folder, f))]\n",
    "\n",
    "    # 设置PPT幻灯片尺寸（单位：厘米）\n",
    "    ppt_width, ppt_height = 25, 10  # 10 inches by 7.5 inches\n",
    "\n",
    "    # 创建一个新的PPT文档\n",
    "    prs = Presentation()\n",
    "    prs.slide_width = Cm(ppt_width)\n",
    "    prs.slide_height = Cm(ppt_height)\n",
    "\n",
    "    # 逐个插入图片到PPT文档中\n",
    "    for idx, image_file in enumerate(image_files, start=1):\n",
    "        image_path = os.path.join(input_folder, image_file)\n",
    "\n",
    "        try:\n",
    "            # 打开图片\n",
    "            image = Image.open(image_path)\n",
    "\n",
    "            # 调整图片大小以适应幻灯片\n",
    "            img_width, img_height = image.size\n",
    "            aspect_ratio = img_width / img_height\n",
    "            slide_aspect_ratio = ppt_width / ppt_height\n",
    "\n",
    "            if aspect_ratio > slide_aspect_ratio:  # 图片更宽，按宽度缩放\n",
    "                new_width = ppt_width\n",
    "                new_height = new_width / aspect_ratio\n",
    "            else:  # 图片更高或等比例，按高度缩放\n",
    "                new_height = ppt_height\n",
    "                new_width = new_height * aspect_ratio\n",
    "\n",
    "            # 在PPT文档中插入一个新的幻灯片\n",
    "            slide = prs.slides.add_slide(prs.slide_layouts[5])  # 使用空白布局\n",
    "            left = (ppt_width - new_width) / 2\n",
    "            top = (ppt_height - new_height) / 2\n",
    "            slide.shapes.add_picture(image_path, left=Cm(left), top=Cm(top), width=Cm(new_width), height=Cm(new_height))\n",
    "\n",
    "            # 获取第一个字符和第三个字符\n",
    "            first_char = image_file[0]\n",
    "            third_char = image_file[2]\n",
    "\n",
    "            # 设置替换文本\n",
    "            replace_X = None\n",
    "            if first_char == '1':\n",
    "                replace_X = \"50\"\n",
    "            elif first_char == '2':\n",
    "                replace_X = \"475\"\n",
    "            elif first_char == '3':\n",
    "                replace_X = \"2475\"\n",
    "            elif first_char == '4':\n",
    "                replace_X = \"10000\"\n",
    "\n",
    "            replace_Y = None\n",
    "            if third_char == '1':\n",
    "                replace_Y = \"高\"\n",
    "            elif third_char == '2':\n",
    "                replace_Y = \"中高\"\n",
    "            elif third_char == '3':\n",
    "                replace_Y = \"中\"\n",
    "            elif third_char == '4':\n",
    "                replace_Y = \"中低\"\n",
    "            elif third_char == '5':\n",
    "                replace_Y = \"低\"\n",
    "\n",
    "            textbox_left = Cm(0.8)\n",
    "            textbox_top = Cm(-0.7)\n",
    "            textbox_width = Cm(ppt_width - 2.0)\n",
    "            textbox_height = Cm(1.0)\n",
    "            textbox = slide.shapes.add_textbox(left=textbox_left, top=textbox_top, width=textbox_width, height=textbox_height)\n",
    "            text_frame = textbox.text_frame\n",
    "            text_frame.word_wrap = True\n",
    "            text_frame.auto_size = True\n",
    "            # text_frame.fill.solid()\n",
    "            # text_frame.fill.fore_color.rgb = RGBColor(255, 255, 255)\n",
    "            \n",
    "            p = text_frame.add_paragraph()\n",
    "            p.text = f\"直接经济损失{replace_Y}风险地区占比（{replace_X}年一遇地震）\"  # 添加文本框内容\n",
    "            p.font.size = Pt(12)  # 设置文字大小\n",
    "            p.font.bold = True  # 设置黑体字体\n",
    "            p.font.color.rgb = RGBColor(0, 0, 0)  # 设置黑色字体颜色\n",
    "            textbox.z_order = 1\n",
    "   \n",
    "        except:\n",
    "            # 忽略非图片文件（如 .DS_Store）\n",
    "            continue\n",
    "\n",
    "    # 保存PPT文档\n",
    "    prs.save(output_ppt)\n",
    "\n",
    "# 调用函数，将指定文件夹中的图片转换为PPT\n",
    "input_folder = \"/Users/wanghao4799/Downloads/images-11(Powered by MaxAI)\"  # 替换为您的图片文件夹路径\n",
    "output_ppt = \"output.pptx\"  # 替换为输出PPT文件名\n",
    "convert_images_to_ppt(input_folder, output_ppt)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.19"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
